package cn.com.idmy.auth.rbac;

import com.alibaba.fastjson2.support.spring6.data.redis.GenericFastJsonRedisSerializer;
import lombok.experimental.Accessors;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

@ConditionalOnClass(io.lettuce.core.RedisClient.class)
@Component
@Accessors(fluent = true)
public class RedisConfig {
    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate string = new StringRedisTemplate();
        string.setConnectionFactory(factory);
        string.afterPropertiesSet();
        return string;
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> object = new RedisTemplate<>();
        object.setConnectionFactory(factory);
        object.setKeySerializer(new StringRedisSerializer());
        object.setHashKeySerializer(new StringRedisSerializer());
        object.setValueSerializer(new GenericFastJsonRedisSerializer());
        object.setHashValueSerializer(new GenericFastJsonRedisSerializer());
        return object;
    }
}
